package com.aix.demo.view;

import com.aix.android.framework.util.ViewUtil;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.Switch;

public class MySwitch extends Switch {
	
	private Paint paint;
	
	private Rect textBounds = new Rect();
	
	private String mText = "列表";
	
	private String mText2 = "地图";
	
	private int x;
	
	private int y;

	public MySwitch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		init();
	}

	public MySwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		init();
	}

	public MySwitch(Context context) {
		super(context);
	}
	
	private void init() {
		paint = new Paint();
		paint.setColor(0xffffffff);
		paint.setTextSize(ViewUtil.convertDipToPixel(15, getContext()));
		
		paint.getTextBounds(mText, 0, mText.length(), textBounds);
		
		x = ViewUtil.convertDipToPixel(25, getContext());

	}
	
	

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(0xffffffff);
		
		super.onDraw(canvas);
		
		int textHeight = textBounds.height();
		int height = getHeight();
		y = (height - textHeight) / 2 + textHeight;
		
		int width = getWidth();
		x = width / 4 - textBounds.width() / 2;
		
		
		if (isChecked()) {
			paint.setColor(0xff000000);
			canvas.drawText("列表", x, y, paint);
			paint.setColor(0xffffffff);
			canvas.drawText(mText, x + width / 2, y, paint);
		} else {
			paint.setColor(0xffffffff);
			canvas.drawText("地图", x, y, paint);
			paint.setColor(0xff000000);
			canvas.drawText(mText2, x + width / 2, y, paint);
		}
		
	}
}
